package se137.hrbust.hlgshopping137.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import jdk.internal.dynalink.support.NameCodec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import se137.hrbust.hlgshopping137.dto.LoginResponse137;
import se137.hrbust.hlgshopping137.entity.User137;
import se137.hrbust.hlgshopping137.service.UserService137;
import se137.hrbust.hlgshopping137.utils.JwtUtil137;
import se137.hrbust.hlgshopping137.utils.Result137;

import java.util.UUID;

@RestController
@Tag(name = "用户管理", description = "用户管理")
@RequestMapping("/user")
public class UserController137 {
    @Autowired
    UserService137 userService;
    @Autowired
    JwtUtil137 jwtUtil;



    @Tag(name = "查询用户", description = "根据id查询用户信息")
    @RequestMapping("/get/{id}")
    public Result137 getUserById(@PathVariable int  id) {
        return new Result137().ok(userService.findUserById(id));
    }

    @PostMapping
    public  Result137  inserUser130(User137 user){
        return  new Result137().ok("success");
    }

    public  Result137  updateUser130(User137 user){
        userService.editUser(user);
        return new Result137().ok("success");
    }

    @RequestMapping ("/delete")
    public Result137 deleteUsers(int[] ids){
        for(int id:ids)
            userService.deleteUser(id);
        return new Result137().ok("删除成功");
    }
@GetMapping("/info")
    public Result137<User137> getUserInfo(@RequestHeader("Authorization") String token){
        if (token !=null && token.startsWith("Bearer ")){
            token = token.substring(7);
        }
        String username = jwtUtil.getUsernameFromToken(token);
        User137 user137 = userService.getUserByUsername(username);
        user137.setUserPass137(null);
        return new Result137().ok(user137);
    }
    @PostMapping("/login")
    @ResponseBody
    public LoginResponse137 login(@RequestBody User137 loginRequest) {
        LoginResponse137 response = new LoginResponse137();
        System.out.println("```````````````"+loginRequest.getUserCode137());
        try {
            User137 user = userService.getUserByUsername(loginRequest.getUserCode137());
            System.out.println("----"+user.getUserPass137());



            if (user != null && user.getUserPass137().equals(loginRequest.getUserPass137())) {
                response.setSuccess(true);
                response.setMessage("登录成功");
                response.setUserId(user.getUserId137());
                response.setUserName(user.getUserName137());
                // 生成简单token（实际项目中应使用JWT等更安全的方式）
                response.setToken(UUID.randomUUID().toString());
            } else {
                response.setSuccess(false);
                response.setMessage("用户名或密码错误");
            }
        } catch (Exception e) {
            response.setSuccess(false);
            response.setMessage("登录异常：" + e.getMessage());
        }

        return response;
    }
    //注册
    @PostMapping("/register")
//    @ResponseBody
    public Result137 register(@RequestBody User137 user137) {
        try {
            userService.register(user137);
            return new Result137().ok("注册成功");
        } catch (Exception e) {
            return new Result137().fail("注册失败：" + e.getMessage());
        }
    }

}